package com.my.controller;

import com.my.service.UserService;
import com.my.vo.AdminLoginVO;
import com.my.vo.StudentLoginVO;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/*
 * @Project:seat-subscribe-sys
 * @Author:cxs
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Controller
public class IndexController {

    @Autowired
    private UserService studentService;

    @RequestMapping("/")
    public String toIndex(Model model) {
        AdminLoginVO loginVO = new AdminLoginVO();
        loginVO.setId(1);
        loginVO.setRole("admin");
        loginVO.setUsername("wahaha");
        model.addAttribute("user",loginVO);
        return "index";
//        return "redirect:/admin/";
    }

    /**
     * Describe: 获取主页视图
     * Param: ModelAndView
     * Return: 登录视图
     */
    @GetMapping("index")
    public ModelAndView index() {
        return jumpPage("index");
    }

    /**
     * Describe: 获取主页视图
     * Param: ModelAndView
     * Return: 主页视图
     */
    @GetMapping("console")
    public ModelAndView home() {
        return jumpPage("console/console");
    }

    /**
     * Describe:无权限页面
     * Return:返回403页面
     */
    @GetMapping("error/403")
    public ModelAndView noPermission() {
        return jumpPage("error/403");
    }

    /**
     * Describe:找不带页面
     * Return:返回404页面
     */
    @GetMapping("error/404")
    public ModelAndView notFound() {
        return jumpPage("error/404");
    }

    /**
     * Describe:异常处理页
     * Return:返回500界面
     */
    @GetMapping("error/500")
    public ModelAndView onException() {
        return jumpPage("error/500");
    }


    @RequestMapping("/welcome")
    public ModelAndView welcome(ModelAndView mv, HttpServletRequest request) {
        AdminLoginVO loginVO = new AdminLoginVO();
        loginVO.setId(1);
        loginVO.setRole("admin");
        loginVO.setUsername("wahaha");
        mv.addObject("user", loginVO);
        mv.setViewName("welcome");
        return mv;
    }

//    @RequestMapping("/welcome")
//    public ModelAndView welcome(ModelAndView mv, HttpSession session, HttpServletRequest request) {
//        Object user = session.getAttribute("loginUser");
//        if (null != user) {
//            if (user instanceof StudentLoginVO) {
//                StudentLoginVO loginVO = (StudentLoginVO) user;
//                List<SeatOrderVO> list = seatOrderService.getReservationBySno(loginVO.getSno(), LocalDate.now());
//                mv.addObject("list", list);
//                mv.addObject("user", loginVO);
//            } else {
//                AdminLoginVO loginVO = (AdminLoginVO) user;
//                mv.addObject("user", loginVO);
//            }
//        }
//        mv.setViewName("welcome");
//        return mv;
//    }mv

    @RequestMapping("/404")
    public ModelAndView error404(ModelAndView mv) {
        mv.setViewName("error/404");
        return mv;
    }

    /**
     * 页面跳转
     */
    public ModelAndView jumpPage(String path) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(path);
        return modelAndView;
    }

    /**
     * 带参数的页面跳转
     */
    public ModelAndView jumpPage(String path, Map<String, ?> params) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(path);
        modelAndView.addAllObjects(params);
        return modelAndView;
    }
}
